/*user login module */

define(function( require , exports ){
	var $ = require('../libs/jquery/jquery');
	var _ = require('../libs/backbone/underscore');
	var Backbone = require('../libs/backbone/backbone');
	require('../libs/jquery/form/jquery.form')($);
	require('../libs/bootstrap/js/bootstrap')($);
	require('../libs/jquery/placeholder/jquery.html5.placeholder')($);
	
	$(function(){
		$('#glodon_fwd_dlg').modal( 
		{
			keyboard: true,
			show:false,
			"backdrop"  : "static"
		});
		
		var user = Backbone.Model.extend({
			onCorrect: function(ele)
			{
				$(ele).next("span").html("<img src="+ $(ele).next("span").attr("data-img") +">");
			},
			
			updateMsg : function( selector , msg ){
				$(selector).html($(selector).attr(msg));
			},
			
			initialize : function(){
				var that = this;
				
				$.ajaxSetup({
            	    global:false,
            	    error : function( XMLHttpRequest, textStatus, errorThrown ){
        	            $('#glodon_error_dlg').modal("show");
            	        $('#glodon_error_dlg .modal-body').html( XMLHttpRequest.responseText );
            	        $('#mask').hide();
            	    }
            	}) 
				$("#glodon_submit a:first").click(function(event){
					if( !$("#userName_str").val() || !$("#password_str").val())
					{
						if( !$("#userName_str").val() )
						{
							that.updateMsg("#userName_str +span" , "data-msg");
							$("#userName_str").parents(".control-group").addClass("error");
							$("#userName_str").focus();
						}
						else if(!$("#userName_str").val().match(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) )
						{
							that.updateMsg("#userName_str +span" , "data-cmsg");
						}
							
						if( !$("#password_str").val() )
						{
							that.updateMsg("#password_str +span" , "data-msg");
							$("#password_str").parents(".control-group").addClass("error");
							$("#password_str").focus();
						}
						return false;
					}
					else if( $("#userName_str +span").text() ){
						$("#userName_str").focus();
						return false;
					}
					else
					{
						$("#glodon_submit a:first").button("loading");
						$("form#glodon_login").submit();
					}
				});
				
				$("#glodon_email").blur(function(){
			    	if( !$(this).val() )
					{
			    		that.updateMsg("#glodon_email +span" , "data-msg");
					}
			    	else if(!$("#glodon_email").val().match(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/))
		    		{
			    		that.updateMsg("#glodon_email +span" , "data-cmsg");
		    		}
			    	else
					{
					    $.post("/account/check.json",{loginName :$("#glodon_email").val() } , function(data){
				            if(eval(data.result))
				            {
					            that.onCorrect($("#glodon_email"));
				            }
				            else
				            {
				                that.updateMsg("#glodon_email +span" , "data-hmsg");
				            }
				        }
				        ,"json"
				        )
					}
				});
				
				$("#glodon_password").change(function(){
			    	if( !$(this).val() )
					{
			    		that.updateMsg("#glodon_password +span" , "data-msg");
					}
			    	else
					{
			    		that.onCorrect(this);
					}
				});
				
				$("#glodon_password_confirm").change(function(){
					if( !$(this).val() )
					{
						that.updateMsg("#glodon_password_confirm +span" , "data-msg" );
					}
			    	else if( $("#glodon_password").val() != $(this).val() )
					{
			    		that.updateMsg("#glodon_password_confirm +span" , "data-cmsg" );
					}
			    	else
					{
						that.onCorrect(this);;
					}
				});
				
				
				$("#userName_str").change(function(){
			    	if( !$(this).val() )
					{
			    		that.updateMsg("#userName_str +span" , "data-msg");
					}
			    	else if(!$("#userName_str").val().match(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/))
		    		{
			    		that.updateMsg("#userName_str +span" , "data-cmsg");
		    		}
			    	else
					{
			    		that.onCorrect(this);;
					}
				});
				
				$("#password_str").change(function(){
			    	if( !$(this).val() )
					{
			    		that.updateMsg("#password_str +span" , "data-msg");
					}
			    	else
					{
			    		that.onCorrect(this);;
					}
				});
				
				$("#glodon_register_container input").keypress(function(e){
				    if(e.keyCode == 13)
				    {
				        $("#glodon_register_submit a").trigger("click");
				    }
				})
				
				$("#glodon_login_container input").keypress(function(e){
				    if(e.keyCode == 13)
				    {
				        $("#glodon_submit a:first").trigger("click");
				    }
				})
				
				$("#glodon_register_submit a").click(function(e){
					e.preventDefault();
					var flag = true;
					$("#glodon_register_container .glodon_error").each(function(){
						if( $(this).text().trim())
						{
							$(this).parents(".control-group").addClass("error");
							flag = false;
						}
					});
					
					$("#glodon_register_container input").each(function(){
							if( !$(this).val() )
							{
								var text = $(this).next().attr('data-msg');
								$(this).parents(".control-group").addClass("error");
								$(this).next().html(text);
								flag = false;
							}
					});
					
					if(flag)
					{
						$("#register_form").submit( );
					}
				})
				
				$("#register_form input").change(function(){
					$(this).parents(".control-group").removeClass("error");
				})
				$("#glodon_login input").change(function(){
					$(this).parents(".control-group").removeClass("error");
				})
				$("#sign_register a").click(function(){
					$("#glodon_login_section").fadeOut(function(){
						$("#glodon_register_section").fadeIn();
					});
					
				})
				
				$("#register_sign a").click(function(){
					$("#glodon_register_section").fadeOut(function(){
						$("#glodon_login_section").fadeIn();
					});
				})
				
				
				if($("#glodon_login_error").hasClass("login_error"))
				{
					$("#notify").addClass("fail");
					$("#notify-msg").html("用户名密码不匹配");
					$("#mask").hide();
					$("#notify-wrapper").fadeIn(1).delay(2000).fadeOut(800 );
				}
				
				$(".fPassword").click(function(e){
				    e.preventDefault();
				    $("#resetFormMsg").empty();
    			    $("#email_success").hide();
            	    $("#sendEmail").show();
				})
				
				$("#resetForm button[type='submit']").click(function(e){
				    e.preventDefault();
				    if( !$("#resetEmail").val().match(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) )
				    {
				        $("#resetEmail").parents(".control-group").addClass("error");
				    }
				    else
				    {
				        $("#mask").show();
				        $("#resetForm").ajaxSubmit({
            				url:"/account/emailResetPasswordLink.json",
            				type:"post",
            				success : function(data){
            				    $("#mask").hide();
            				    if( data.result == "fail" )
            				    {
            				        $("#resetFormMsg").html($("#resetFormMsg").attr("data-fmsg")).css({color:"red"});
            				    }else if(!eval(data.result))
            				    {
            				        $("#resetFormMsg").html($("#resetFormMsg").attr("data-msg")).css({color:"red"});
            				    }
            				    else
            				    {
            				        
            				        $("#email_success").show();
            				        $("#sendEmail").hide();
            				    }
            				}
            			});
				    }
				})
				$("#resetEmail").change(function(){
				    $(this).parents(".control-group").removeClass("error");
				})
				
				$("#forPWD input").change(function(){
				    $(this).parents(".control-group").removeClass("error");
				})
				
				$("#forPWD button[type='submit']").click(function(e){
				    e.preventDefault();
				    var flag = true;
					$("#forPWD .glodon_error").each(function(){
						if( $(this).text().trim())
						{
							$(this).parents(".control-group").addClass("error");
							flag = false;
						}
					});
					
					$("#forPWD input").each(function(){
							if( !$(this).val() )
							{
								var text = $(this).next().attr('data-msg');
								$(this).parents(".control-group").addClass("error");
								$(this).next().html(text);
								flag = false;
							}
					});
					
				    if( flag )
				    {
				        $("#mask").show();
				        $("#forPWD form").ajaxSubmit({
            				url:"/account/changePassword.json",
            				type:"post",
            				success : function(data){
            				    $("#mask").hide();
            				    if(eval(data.result))
            				    {
                				    $("#forPWD").hide(); 
                				    $("#sucPWD").show();
                				    (function jump(count) {    
                                        window.setTimeout(function(){    
                                            count--;    
                                            if(count > 0) {    
                                                $('#count').text( count );    
                                                jump(count);    
                                            } else {    
                                                location.href=window._global["contextPath"];    
                                            }    
                                        }, 1000);    
                                    })(parseInt($("#count").text()));   
            				    }
            				    else
            				    {
            				        $("#forPWD").hide(); 
            				        $("#failPWD").show();
            				        (function jump(count) {    
                                        window.setTimeout(function(){    
                                            count--;    
                                            if(count > 0) {    
                                                $('#fcount').text( count );    
                                                jump(count);    
                                            } else {    
                                                location.href=window._global["contextPath"];    
                                            }    
                                        }, 1000);    
                                    })(parseInt($("#fcount").text()));   
            				    }
            				    
            				}
            			});
				    }
				})
				
				$("#npassword").change(function(){
			    	if( !$(this).val() )
					{
			    		that.updateMsg("#npassword +span" , "data-msg");
					}
			    	else
					{
			    		that.onCorrect(this);
					}
				});
				
				$("#cpassword").change(function(){
					if( !$(this).val() )
					{
						that.updateMsg("#cpassword +span" , "data-msg" );
					}
			    	else if( $("#npassword").val() != $(this).val() )
					{
			    		that.updateMsg("#cpassword +span" , "data-cmsg" );
					}
			    	else
					{
						that.onCorrect(this);;
					}
				});
			}
		})
		var userInstance = new user;
	})
	
})
